﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.Data.SqlClient;
using OneDrive.Model;
using Maticsoft.DBUtility;

namespace OneDrive.DAO
{
    public partial class AgentInfoDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("AgentID", "tbl_AgentInfo");
        }

        public static bool Clear()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_AgentInfo ");

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int iAgentID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbl_AgentInfo");
            strSql.Append(" where AgentID=@AgentID");
            SqlParameter[] parameters = {
                    new SqlParameter("@AgentID", SqlDbType.Int)
            };
            parameters[0].Value = iAgentID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(AgentInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbl_AgentInfo(");
            strSql.Append("AgentID,Name,Sex,Phone,Rank,PortertyFee,Company,Outlets)");
            strSql.Append(" values (");
            strSql.Append("@AgentID,@Name,@Sex,@Phone,@Rank,@PortertyFee,@Company,@Outlets)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@AgentID", SqlDbType.Int),
                    new SqlParameter("@Name", SqlDbType.NVarChar,50),
                    new SqlParameter("@Sex", SqlDbType.Int),
                    new SqlParameter("@Phone", SqlDbType.VarChar,15),
                    new SqlParameter("@Rank", SqlDbType.Float),
                    new SqlParameter("@Company", SqlDbType.NVarChar,50),
                    new SqlParameter("@Outlets", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.AgentID;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Sex;
            parameters[4].Value = model.Phone;
            parameters[5].Value = model.Rank;
            parameters[7].Value = model.Company;
            parameters[8].Value = model.Outlets;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(AgentInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbl_AgentInfo set ");
            strSql.Append("AgentID=@AgentID,");
            strSql.Append("Name=@Name,");
            strSql.Append("Sex=@Sex,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Rank=@Rank,");
            strSql.Append("Company=@Company,");
            strSql.Append("Outlets=@Outlets,");

            SqlParameter[] parameters = {
                    new SqlParameter("@AgentID", SqlDbType.Int),
                    new SqlParameter("@Name", SqlDbType.NVarChar,50),
                    new SqlParameter("@Sex", SqlDbType.Int),
                    new SqlParameter("@Phone", SqlDbType.VarChar,15),
                    new SqlParameter("@Rank", SqlDbType.Float),
                    new SqlParameter("@Company", SqlDbType.NVarChar,50),
                    new SqlParameter("@Outlets", SqlDbType.NVarChar,20)};

            parameters[1].Value = model.AgentID;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Sex;
            parameters[4].Value = model.Phone;
            parameters[5].Value = model.Rank;
            parameters[7].Value = model.Company;
            parameters[8].Value = model.Outlets;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int iAgentID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_AgentInfo ");
            strSql.Append(" where AgentID=@AgentID");
            SqlParameter[] parameters = {
                    new SqlParameter("@AgentID", SqlDbType.Int)
            };
            parameters[0].Value = iAgentID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string strAgentIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_AgentInfo ");
            strSql.Append(" where AgentID in (" + strAgentIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public AgentInfo GetModel(int iAgentID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AgentID,Name,Sex,Phone,Rank,Company,Outlets from tbl_AgentInfo ");
            strSql.Append(" where AgentID=@AgentID");
            SqlParameter[] parameters = {
                    new SqlParameter("@AgentID", SqlDbType.Int)
            };
            parameters[0].Value = iAgentID;

            AgentInfo model = new AgentInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public AgentInfo DataRowToModel(DataRow row)
        {
            AgentInfo model = new AgentInfo();
            if (row != null)
            {
                if (row["AgentID"] != null && row["AgentID"].ToString() != "")
                {
                    model.AgentID = int.Parse(row["AgentID"].ToString());
                }
                if (row["AgentID"] != null)
                {
                    model.Name = row["AgentID"].ToString();
                }
                if (row["Sex"] != null)
                {
                    model.Sex = int.Parse(row["Sex"].ToString());
                }
                if (row["Phone"] != null && row["Phone"].ToString() != "")
                {
                    model.Phone = row["Phone"].ToString();
                }
                if (row["Rank"] != null)
                {
                    model.Rank = float.Parse(row["Rank"].ToString());
                }
                if (row["Company"] != null && row["Company"].ToString() != "")
                {
                    model.Company = row["Company"].ToString();
                }
                if (row["Outlets"] != null)
                {
                    model.Outlets = row["Outlets"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AgentID,Name,Sex,Phone,Rank,Company,Outlets ");
            strSql.Append(" FROM tbl_AgentInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" AgentID,Name,Sex,Phone,Rank,Company,Outlets ");
            strSql.Append(" FROM tbl_AgentInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbl_AgentInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.CID desc");
            }
            strSql.Append(")AS Row, T.*  from Course T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<AgentInfo> DataTableToList(DataTable dt)
        {
            List<AgentInfo> modelList = new List<AgentInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                AgentInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        #endregion  BasicMethod

    }
}
